package cn.zust.edu.cn;

public class Triangle implements IShape{
    private Point ptA;
    private Point ptB;
    private Point ptC;

    private final double edgeA;
    private final double edgeB;
    private final double edgeC;

    public Triangle(Point ptA, Point ptB, Point ptC) {
        this.ptA = ptA;
        this.ptB = ptB;
        this.ptC = ptC;

        this.edgeA = calculateDistance(ptB, ptC);
        this.edgeB = calculateDistance(ptA, ptC);
        this.edgeC = calculateDistance(ptA, ptB);
    }

    private double calculateDistance(Point p1, Point p2) {
        return Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) + Math.pow(p2.getY() - p1.getY(), 2));
    }


    @Override
    public double perimeter() {
        return edgeA+edgeB+edgeC;
    }

    @Override
    public double area() {
        double s = perimeter() / 2;
        return Math.sqrt(s * (s - edgeA) * (s - edgeB) * (s - edgeC));
    }

    @Override
    public void draw() {
        System.out.println("三角形顶点为："+ptA.getX()+","+ptA.getY()+"，"+ptB.getX()+","+ptB.getY()+"，"+ptC.getX()+","+ptC.getY());
    }
}
